﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.SimEvents;

namespace BuildPlannerLib.PlayerActions
{
    // Derived class of Action for sending mineral workers to "Idle" (scouting, etc.)
    public class IdleWorker : PlayerAction
    {
        // Field for the base from which the worker will be pulled
        public Base Base;

        // Constructor with parameters for base and time
        public IdleWorker(Build build, Base b, int timeDuration)
        {
            Build = build;
            Base = b;
            TimeDuration = timeDuration;
            Description = "Idle Worker (Base " + Build.Map.Bases.IndexOf(Base) + ")";
            LongDescription = "Idling a worker from base number " + Build.Map.Bases.IndexOf(Base) + " for " + TimeConvert.ToTimeSpan(TimeDuration).ToString("g") +".";
        }

        // Evaluates whether there are workers mining minerals at the base
        public override bool RequirementsFilled()
        {
            Error = null;

            if (Base.MineralWorkers == 0)
            {
                Error = "No workers assigned to minerals at the specified base.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if there is a worker in the Leaving, Close, or Idle categories
        public override bool StartCheck()
        {
            if (Base.MineralWorkerStatus.Sum() == 0)
                return false;

            return base.StartCheck();
        }

        // Removes the worker from minerals and adds the event for finishing the action
        public override void StartAction()
        {
            base.StartAction();

            Base.RemoveMineralWorker(ref Build);
            Build.Player.IdleWorkers++;
        }

        // Adds the worker back to the base from which it came
        public override void FinishAction()
        {
            base.FinishAction();

            Build.Player.IdleWorkers--;
            Base.AddMineralWorker(ref Build);
        }
    }
}
